//
//  PopCashView.m
//  location
//
//  Created by Dongxiang Xu on 2025/10/31.
//

#import "PopCashView.h"

@implementation PopCashView

- (instancetype)initWithFrame:(CGRect)frame
                    GroupInfo:(NSDictionary *)groupInfo
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initViewWithGroupInfo:groupInfo];
    }
    return self;
}

- (void)initViewWithGroupInfo:(NSDictionary *)groupInfo
{
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self addSubview:bg];
        
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    infoView.layer.masksToBounds = YES;
    [infoView.layer setCornerRadius:32];
    infoView.backgroundColor = CashColor;
    [self addSubview:infoView];
    
    UILabel *slider = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-42)/2, 8, 42, 4)];
    slider.layer.masksToBounds = YES;
    slider.backgroundColor =  GraryMiddleColor;
    [slider.layer setCornerRadius:2];
    [infoView addSubview:slider];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(16, 50, SCREEN_WIDTH-32, 36)];
    title.textColor = RealWhiteColor;
    title.font = MEDIUM_FONT(28);
    title.text = @"更多的位置提醒";
    [infoView addSubview:title];
    
    UILabel *subTitle = [[UILabel alloc] initWithFrame:CGRectMake(16, title.maxY+16, infoView.width-32, 24)];
    subTitle.textColor = [RealWhiteColor colorWithAlphaComponent:0.75];
    subTitle.font = DEFAULT_FONT(14);
    subTitle.text = @"接收家庭最喜欢地点的提醒，开通会员获得更多提醒、更多安全功能。";
    subTitle.numberOfLines = 0;
    [subTitle sizeToFit];
    [infoView addSubview:subTitle];
    
    UILabel *current = [[UILabel alloc] initWithFrame:CGRectMake(16, subTitle.maxY+32, 82, 24)];
    current.textColor = [RealWhiteColor colorWithAlphaComponent:0.6];
    current.font = DEFAULT_FONT(14);
    current.text = @"当前计划";
    [infoView addSubview:current];
    
    UILabel *currentDetail = [[UILabel alloc] initWithFrame:CGRectMake(16, current.maxY+8, 82, 24)];
    currentDetail.textColor = [RealWhiteColor colorWithAlphaComponent:0.6];
    currentDetail.font = DEFAULT_FONT(14);
    currentDetail.text = @"2个位置提醒";
    [infoView addSubview:currentDetail];
    
    UILabel *member = [[UILabel alloc] initWithFrame:CGRectMake(infoView.width-102, subTitle.maxY+32, 58, 24)];
    member.font = REAL_MEDIUM_FONT(14);
    member.textAlignment = NSTextAlignmentRight;
    [infoView addSubview:member];
    
    UIImageView *memberImg = [[UIImageView alloc] initWithFrame:CGRectMake(member.maxX+6, subTitle.maxY+33, 22, 22)];
    [infoView addSubview:memberImg];
    
    UILabel *locationCount = [[UILabel alloc] initWithFrame:CGRectMake(infoView.width-118, memberImg.maxY+8, 102, 24)];
    locationCount.font = REAL_MEDIUM_FONT(14);
    locationCount.textAlignment = NSTextAlignmentRight;
    locationCount.textColor = RealWhiteColor;
    [infoView addSubview:locationCount];
        
    if ([groupInfo[@"memberCount"] integerValue]<=2) {
        member.text = @"亲密会员";
        member.textColor = CloseColor;
        memberImg.image = [UIImage imageNamed:@"icon_close"];
        locationCount.text = @"10个位置提醒";
    }else{
        member.text = @"黄金会员";
        member.textColor = GoldColor;
        memberImg.image = [UIImage imageNamed:@"icon_gold"];
        locationCount.text = @"无限制位置提醒";
    }
    
    UIButton *pay = [[UIButton alloc] initWithFrame:CGRectMake(16, locationCount.maxY+26, infoView.width-32, 54)];
    pay.backgroundColor = NormalMainColor;
    pay.layer.masksToBounds = YES;
    [pay.layer setCornerRadius:27];
    [pay setTitleColor:RealWhiteColor forState:UIControlStateNormal];
    [pay.titleLabel setFont:MEDIUM_FONT(16)];
    [pay setTitle:@"开通会员" forState:UIControlStateNormal];
    [infoView addSubview:pay];
    [[pay rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (self.PopCashBlock) {
            self.PopCashBlock();
        }
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [UIColor clearColor];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, pay.maxY+10, infoView.width-32, 16)];
    infoLabel.font = DEFAULT_FONT(12);
    infoLabel.textAlignment = NSTextAlignmentCenter;
    infoLabel.textColor = [RealWhiteColor colorWithAlphaComponent:0.65];
    infoLabel.text = [NSString stringWithFormat:@"为 “%@” 开通",groupInfo[@"name"]];
    
    [infoView addSubview:infoLabel];
    
    [UIView animateWithDuration:0.3 animations:^{
        infoView.y = SCREEN_HEIGHT-386;
        self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0.4];
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [UIColor clearColor];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    [bg addGestureRecognizer:tap];
}



@end
